<template>
  <div class="my-container">
    <div class="header">
      <div class="header-login" v-if="userToken !== null">
        <div class="header-wrap">
        <van-cell
          center
        >
          <!-- 使用 title 插槽来自定义左侧 -->
          <template #title>
            <div class="head-portrait-wrap">
              <img src="userInfo.photo" alt="" class="img">
              <span class="name">{{userInfo.name}}</span>
            </div>
          </template>
          <!-- 使用 right-icon 插槽来自定义右侧 -->
          <template #right-icon>
            <van-button class="right-btn" plain type="primary">编辑资料</van-button>
          </template>
        </van-cell>
      </div>
      <div class="header-item">
        <div class="item">
          <p class="number">{{userInfo.art_count}}</p>
          <h6 class="item-name">头条</h6>
        </div>
        <div class="item">
          <p class="number">{{userInfo.follow_count}}</p>
          <h6 class="item-name">关注</h6>
        </div>
        <div class="item">
          <p class="number">{{userInfo.fans_count}}</p>
          <h6 class="item-name">粉丝</h6>
        </div>
        <div class="item">
          <p class="number">{{userInfo.like_count}}</p>
          <h6 class="item-name">获赞</h6>
        </div>
      </div>
      </div>
      <div class="no-login" v-else>
        <div class="touxiang">
          <img src="./img/iphone.png" alt="" class="touxiang-img">
        </div>
        <p class="nologin-btn" @click="$router.push('/login')">登录 / 注册</p>
      </div>
    </div>
      <!-- // 收藏与历史 -->
    <van-grid clickable :column-num="2">
      <van-grid-item icon-prefix="iconfont"  icon="shoucang" text="收藏" to="/" />
      <van-grid-item icon-prefix="iconfont"  icon="lishi" text="历史" url="/vant/mobile.html" />
    </van-grid>
    <van-cell class="handle-item" title="消息通知" is-link />
    <van-cell class="handle-item handle-item-esp" title="小智同学" is-link />
    <van-button class="login-btn" block :hairline="true" type="primary" v-if="userToken !== null" @click="loginOut">退出登录</van-button>
  </div>
</template>

<script>
import { mapState, mapMutations } from 'vuex'
import { requestUserProfile } from '@/api/user'
export default {
  name: 'My',
  data () {
    return {
      // 用户信息
      userInfo: {}
    }
  },
  created () {
    this.getUserPrifile()
  },
  mounted () {
    console.log(this.userToken)
  },
  computed: {
    // userToken用户登录信息，如果为null代表未登录
    ...mapState(['userToken'])
  },
  methods: {
    ...mapMutations([
      'STORAGE_TOKEN'
    ]),
    // 获取用户信息
    async getUserPrifile () {
      try {
        const { data } = await requestUserProfile()
        console.log(data)
        this.userInfo = data.data
      } catch {
        console.log('获取失败')
      }
      // requestUserProfile().then(res => {
      //   console.log(res)
      //   this.userInfo = res.data.data
      // }).catch(err => {
      //   console.log(err, '错误')
      // })
    },
    loginOut () {
      this.$dialog.confirm({
        title: '退出确认',
        message: '弹窗内容'
      })
        .then(() => {
          // on confirm
          // 退出登录，重置userToken, 重置userInfo
          this.STORAGE_TOKEN(null)
          this.userInfo = {}
        })
        .catch(() => {
          // on cancel
        })
    }
  }
}

</script>
<style scoped lang="less">
.header {
  height: 200px;
  background: url('./img/banner.png') no-repeat;
  background-size: cover;
  .van-cell {
    background-color: transparent;
    padding: 38px 16px 0;
    .head-portrait-wrap {
      display: flex;
      align-items: center;
      .img {
        width: 66px;
        height: 66px;
        border: 2px solid #fff;
        border-radius: 50%;
        margin-right: 11px;
      }
      .name {
        font-size: 15px;
        color: #FFFFFF;
      }
    }
    .right-btn {
      width: 62px;
      height: 16px;
      line-height: 16px;
      padding: 0;
      font-size: 10px;
      color: #666666;
      border: none;
      border-radius: 18px;
    }
  }
  .header-item {
    display: flex;
    padding-top: 27px;
    .item {
      flex: 1;
      text-align: center;
      color: #fff;
      .number {
        font-size: 18px;
        line-height: 18px;
        height: 18px;
        margin: 0;
      }
      .item-name {
        margin: 0;
        font-size: 12px;
        padding-top: 10px;
      }
    }
  }
  .no-login {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding-top: 47px;
    .touxiang {
      width: 66px;
      height: 66px;
      border-radius: 50%;
      background-color: #fff;
      display: flex;
      justify-content: center;
      align-items: center;
      .touxiang-img {
        width: 26px;
        height: 34px;
      }
    }
    .nologin-btn {
      background-color: transparent;
      font-size: 14px;
      color: #FFFFFF;
      padding-top: 8px;
      // letter-spacing: 3px;
    }
  }
}
.van-grid-item {
  margin-bottom: 5px;
  /deep/ .van-grid-item__content {
    padding-top: 14px;
    padding-bottom: 12px;
    .van-grid-item__icon {
      font-size: 23px;
    }
    .iconfont-shoucang {
      color: #EB5253;
    }
    .iconfont-lishi {
      color: #FF9D1D;
    }
    .van-grid-item__text {
      font-size: 14px;
      letter-spacing: .5em;
      text-indent: .5em;
      color: #333333;
      margin-top: 7px;
      line-height: 14px;
    }
    &::after {
      border-width: 0 1px 0 0;
      transform: scale(.5, .3);
    }
  }
}
.handle-item {
  line-height: unset;
  padding: 18px 16px;
  color: #333333;
  &.handle-item-esp::after {
    border-bottom: none;
  }
  .van-icon {
    height: unset;
    line-height: unset;
    font-size: 11px;
  }
}
.login-btn {
  margin-top: 5px;
  background-color: #fff;
  font-size: 15px;
  color: #D86262;
  &::after {
    border: none;
  }
}
</style>
